package com.android.sq.zouqi.bdmap;

import android.app.Application;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.android.sq.zouqi.Constants;
import com.android.sq.zouqi.main.pScrMain;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;

public class BMapApp extends Application {
	private final static String TAG = "BMapApp";

	// 存储当前显示激活中的界面信息,即当前是哪个Activity在活动
	private int activityName = 0;
	private Object activityInfo;
	
	static BMapApp mDemoApp;
	
	// 这些都是在各自onCreate方法中进行赋值的
	public static pScrMain scrMain;
	public static pScrEventMap scrEventMap;
	
	//百度MapAPI的管理类
	BMapManager mBMapMan = null;
	
	// 授权Key
	// TODO: 请输入您的Key,
	// 申请地址：http://dev.baidu.com/wiki/static/imap/key/
	//String mStrKey = "152C1F60D10F742C1684A9080BF0CDB01303B550";
	String mStrKey = "152C1F60D10F742C1684A9080BF0CDB01303B550";
	boolean m_bKeyRight = true;	// 授权Key正确，验证通过
	static int mNetError = 0;
	static int mNetError_Code = 2;
	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	
	public Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			int action = msg.what;
			String msgContent = "";
			switch (action) {
			case Constants.notify_show_history_signal:
				
				break;
			case Constants.notify_new_event_signal:
				Log.e(TAG, "App - handler: notify_new_event_signal");
				msgContent = (String)msg.obj;
				scrMain.notifyNewEvent(msgContent);
				break;
			case Constants.notify_event_change_signal:
				
				break;
			case Constants.notify_participant_cur_location:
				// 当APP的handler收到更新好友当前位置时，更新好友在地图上的位置
				Log.e(TAG, "App 收到 更新参与人地理方位的推送");
				msgContent = (String) msg.obj;
				scrEventMap.updateFriendLocation(msgContent);
				break;
			}
		}
	};
	
	static class MyGeneralListener implements MKGeneralListener {
		@Override
		public void onGetNetworkState(int iError) {
			Log.e(TAG, "MyGeneralListener->onGetNetworkState error is "+ iError);
			mNetError = iError;
			Toast.makeText(BMapApp.mDemoApp.getApplicationContext(), "您的网络出错啦！",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onGetPermissionState(int iError) {
			Log.e("MyGeneralListener", "onGetPermissionState error is "+ iError);
			if (iError ==  MKEvent.ERROR_PERMISSION_DENIED) {
				// 授权Key错误：
				Toast.makeText(BMapApp.mDemoApp.getApplicationContext(), 
						"请在BMapApiDemoApp.java文件输入正确的授权Key！",
						Toast.LENGTH_LONG).show();
				BMapApp.mDemoApp.m_bKeyRight = false;
			}
			Log.e(TAG, "onGetPermissionState");
		}
	}
	
	@Override
    public void onCreate() {
		mDemoApp = this;
		mBMapMan = new BMapManager(this);
		MyGeneralListener listener = new MyGeneralListener();
		boolean flag = mBMapMan.init(this.mStrKey, listener);
		Log.e(TAG, "mBMapMan->flag = " + flag);
		mBMapMan.getLocationManager().setNotifyInternal(10, 5);
//		if (mBMapMan != null) {
//			mBMapMan.destroy();
//			mBMapMan = null;
//		}
		
		super.onCreate();
		Log.e(TAG, "BMappApp onCreate");
	}

	@Override
	//建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
	public void onTerminate() {
		// TODO Auto-generated method stub
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onTerminate();
		Log.e(TAG, "onTerminate");
	}

	public void setActivityInfo(int activityName, Object info) {
		this.activityName = activityName;
		this.activityInfo = info;
	}
	
	public Object getActivityInfo() {
		return activityInfo;
	}
	
	public int getActivityName() {
		return activityName;
	}
}